home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / amc / amc_install.exe / {app} / Scripts / All Movie Guide (pic).ifs < prev    next >
Encoding:
Text File  |  2005-03-12  |  8.7 KB  |  250 lines

  1. (***************************************************
  2.  
  3. Ant Movie Catalog importation script
  4. www.antp.be/software/moviecatalog/
  5.  
  6. [Infos]
  7. Authors=Hubert Kosior
  8. Title=All Movie Guide
  9. Description=All Movie Guide (US) import
  10. Site=http://allmovie.com
  11. Language=EN
  12. Version=
  13. Requires=3.5.0
  14. Comments=send bugs and reports to: hubert@tm1.net|a bug corrected by Antoine Potten|to do:| - producer's name instad of producing company| - display movie categories when movie list hit (after searching)
  15. License=This program is free software; you can redistribute it and/or modify it under the  terms of the GNU General Public License as published by the Free Software Foundation;  either version 2 of the License, or (at your option) any later version. |
  16. GetInfo=1
  17.  
  18. [Options]
  19.  
  20. ***************************************************)
  21.  
  22. program AllMovie;
  23. var
  24.   MovieName: string;
  25.  
  26. // simple string procedures
  27. function StringReplaceAll(S, Old, New: string): string;
  28. begin
  29.   while Pos(Old, S) > 0 do
  30.     S := StringReplace(S, Old, New);
  31.   Result := S;
  32. end;
  33. procedure CutAfter(var Str: string; Pattern: string);
  34. begin
  35.   Str := Copy(str, Pos(Pattern, Str) + Length(Pattern), Length(Str));
  36. end;
  37. procedure CutBefore(var Str: string; Pattern: string);
  38. begin
  39.   Str := Copy(Str, Pos(Pattern, Str), Length(Str));
  40. end;
  41.  
  42. // Loads and analyses page from internet (list of movies or direct hit)
  43. procedure AnalyzePage(Address: string);
  44. var
  45.   Page: TStringList;
  46. begin
  47.   Page := TStringList.Create;
  48.   Page.Text := GetPage(Address);
  49.   // movie list
  50.   if Pos('movie titles like: ', Page.Text) > 0 then
  51.   begin
  52.     PickTreeClear;
  53.     PickTreeAdd('Search results', '');
  54.     AddMoviesTitles(Page);
  55.     if PickTreeExec(Address) then
  56.       AnalyzePage(Address);
  57.   // refine search
  58.   end
  59.   else
  60.   if Pos('Sorry, there is too many possible matches, please adjust your search.', Page.Text) > 0 then
  61.   begin
  62.     ShowMessage('Sorry, there is too many possible matches, please adjust your search.');
  63.     if Input('All Movie Import', 'Enter the title of the movie:', MovieName) then
  64.       AnalyzePage('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
  65.   // direct hit
  66.   end
  67.   else
  68.   begin
  69.     SetField(FieldURL, Address);
  70.     AnalyzeMoviePage(Page)
  71.   end;
  72. end;
  73.  
  74. // Extracts movie details from page
  75. procedure AnalyzeMoviePage(MoviePage: TStringList);
  76. var
  77.   Page: string;
  78.   Value: string;
  79. begin
  80.   Page := MoviePage.Text;
  81.  
  82.   // Original title
  83.   SetField(fieldOriginalTitle, GetStringFromHTML(Page, '<TITLE>', ': ', '</TITLE>'));
  84.  
  85.   // Year
  86.   SetField(fieldYear, GetStringFromHTML(Page, '<B>'+GetField(fieldOriginalTitle)+'</B>', '</TR>', '</B>'));
  87.  
  88.   // Country
  89.   SetField(fieldCountry, GetStringFromHTML(Page, '<B>'+GetField(fieldOriginalTitle)+'</B>', '<I>', '</I>'));
  90.  
  91.   // Length
  92.   SetField(fieldLength, GetStringFromHTML(Page, '<B>'+GetField(fieldOriginalTitle)+'</B>', '</I> - ', ' min'));
  93.  
  94.   // AKA -> translated title
  95.   SetField(fieldTranslatedTitle, GetStringFromHTML(Page, '>AKA', '</TD>', '</td>'));
  96.  
  97.   // Rating (multiplied by 2, because 0 <= AMG rating <= 5)
  98.   Value := GetStringFromHTML(Page, '>AMG Rating', 'alt="', ' Stars');
  99.   if Length(Value) > 0 then SetField(fieldRating, FloatToStr(StrToFloat(Value)*2));
  100.  
  101.   // Director
  102.   SetField(fieldDirector, GetStringFromHTML(Page, '>Director', '</TD>', '</td>'));
  103.  
  104.   // Genre -> category
  105.   SetField(fieldCategory, GetStringFromHTML(Page, '>Genre/Type', '</TD>', '</td>'));
  106.  
  107.   // Producing company  -> producer
  108.   SetField(fieldProducer, GetStringFromHTML(Page, '>Produced by', '</TD>', '</TD>'));
  109.  
  110.   // Image
  111.   Value := GetStringFromHTML(Page, 'http://image.allmusic.com', '', '"');
  112.   if Length(Value) > 0 then GetPicture(Value);
  113.  
  114.   // Plot synopsis -> description
  115.   Value := GetStringFromHTML(Page, '<A Name="PLOT">', '</table>', '</table>');
  116.   if Length(Value) > 0 then SetField(fieldDescription, 'PLOT SYNOPSIS:'+#13#10+Value+#13#10);
  117.  
  118.   // Review -> description
  119.   Value := GetStringFromHTML(Page, '<A Name="REVIEW">', '</table>', '</table>');
  120.   if Length(Value) > 0 then SetField(fieldDescription, GetField(fieldDescription)+'AMG REVIEW:'+#13#10+Value+#13#10);
  121.  
  122.   // Awards -> description
  123.   // adjust spaces and line feeds
  124.   Value := StringReplaceAll(Page, '> <FONT', ''); // space before title
  125.   Value := StringReplaceAll(Value, '</FONT> </td><td WIDTH=209>', ' - '); // minus before name
  126.   Value := StringReplaceAll(Value, ' </A></FONT></td>', ' - '); // minus after name (1)
  127.   Value := StringReplaceAll(Value, ' </FONT></td>', ' - '); // minus after name (2)
  128.   Value := StringReplaceAll(Value, '</FONT> </td></tr>', + #13#10); // newline after academy name
  129.   Value := GetStringFromHTML(Value, '<A Name="AWRD">', '</td></tr>', '</TABLE>');
  130.   Value := StringReplaceAll(Value, '  ', ' ');
  131.   Value := StringReplaceAll(Value, ' - - ', ' - ');
  132.   if Length(Value) > 0 then SetField(fieldDescription, GetField(fieldDescription)+'AWARDS:'+#13#10+Value);
  133.  
  134.   // remove trailing newline from description
  135.   Value := GetField(fieldDescription);
  136.   if Copy(Value, Length(Value) - 1, 2) = #13#10 then begin
  137.     Value := Copy(Value, 0, Length(Value) - 2);
  138.     SetField(fieldDescription, Value);
  139.   end;
  140.  
  141.   // Cast -> actors
  142.   // adjust semicolons
  143.   Value := StringReplaceAll(Page, '</I></TD></TR>', '; ');
  144.   Value := GetStringFromHTML(Value, '<A Name="CAST">', '</td></tr>', '</TABLE>');
  145.   if Length(Value) > 0 then begin
  146.     // remove double spaces if only actor name given
  147.     while Pos('  ', Value) > 0 do
  148.       Delete(Value, Pos('  ', Value), 2);
  149.     // remove trailing "; "
  150.     if Copy(Value, Length(Value) - 1, 2) = '; ' then
  151.       Value := Copy(Value, 0, Length(Value) - 2);
  152.     SetField(fieldActors, Value)
  153.   end;
  154.  
  155.   //DisplayResults;
  156. end;
  157.  
  158. // Adds movie titles from search results to tree
  159. procedure AddMoviesTitles(ResultsPage: TStringList);
  160. var
  161.   Page: string;
  162.   MovieTitle, MovieAddress: string;
  163. begin
  164.   Page := ResultsPage.Text;
  165.   // Every movie entry begins with string "<A HREF='/cg/avg.dll?"
  166.   while Pos('<A HREF="/cg/avg.dll?', Page) > 0 do
  167.   begin
  168.     CutBefore(Page, '<A HREF="/cg/avg.dll?');
  169.     MovieAddress := 'http://allmovie.com' + GetStringFromHTML(Page, '<A', '"', '">');
  170.     MovieTitle := GetStringFromHTML(Page, '<A', '', '</tr>');
  171.     MovieTitle := StringReplace(MovieTitle, ')', '),  ');
  172.     CutAfter(Page, '</tr>');
  173.     // add movie to list
  174.     PickTreeAdd(MovieTitle, MovieAddress);
  175.   end;
  176. end;
  177.  
  178. // Extracts single movie detail (like director, genre) from page
  179. function GetStringFromHTML(Page, StartTag, CutTag, EndTag: string): string;
  180. begin
  181.   Result := '';
  182.   // recognition tag - if present, extract detail from page, otherwise assume detail is not present
  183.   if Pos(StartTag, Page) > 0 then begin
  184.     CutBefore(Page, StartTag);
  185.     // optional cut tag helps finding right string in html page
  186.     if Length(CutTag) > 0 then
  187.       CutAfter(Page, CutTag);
  188.     // movie detail copied with html tags up to end string
  189.     Result := Copy(Page, 0, Pos(EndTag, Page) - 1);
  190.     // remove html tags and decode html string
  191.     HTMLRemoveTags(Result);
  192.     HTMLDecode(Result);
  193. //  ShowMessage('DEBUG: GetStringFromHTML - StartTag "'+StartTag+'", CutTag "'+CutTag+'", EndTag "'+EndTag+'", Result "'+Result+'" ___ '+Page);
  194.   end;
  195. end;
  196.  
  197. procedure RemovePronoun(var Str: string);
  198. var
  199.   i: Integer;
  200.   s: string;
  201.   c: char;
  202. begin
  203.   // remove pronouns
  204.   s := UpperCase(Copy(Str, 0, 4));
  205.   if (s = 'LES ') or (s = 'UNE ') or (s = 'THE ') then
  206.     Str := Copy(Str, 5, Length(Str) - 4)
  207.   else
  208.   begin
  209.     s := Copy(s, 0, 3);
  210.     if (s = 'LE ') or (s = 'UN ') then
  211.       Str := Copy(Str, 4, Length(Str) - 3)
  212.     else
  213.     begin
  214.       s := Copy(s, 0, 2);
  215.       if (s = 'L''') or (s = 'L ') or (s = 'A ') then
  216.         Str := Copy(Str, 3, Length(Str) - 2)
  217.     end;
  218.   end;
  219.   // remove non-letters, non-digits and non-spaces
  220.   s := '';
  221.   for i := 1 to Length(Str) do begin
  222.   c := StrGet(Str, i);
  223.     if ((c<'a') or (c>'z')) and
  224.        ((c<'A') or (c>'Z')) and
  225.        ((c<'0') or (c>'9')) and
  226.        (c<>' ') then
  227.     else
  228.       s := s + Copy(Str, i, 1);
  229.   end;
  230.   Str := s;
  231. end;
  232.  
  233. begin
  234.   if CheckVersion(3,5,0) then begin
  235.     MovieName := GetField(fieldOriginalTitle);
  236.     if MovieName = '' then MovieName := GetField(fieldTranslatedTitle);
  237.     if Input('All Movie Import', 'Enter the title of the movie (only letters, digits and spaces):', MovieName) then
  238.     begin
  239.       if Pos('allmovie.com', MovieName) > 0 then
  240.         AnalyzePage(MovieName)
  241.       else
  242.       begin
  243.         RemovePronoun(MovieName);
  244.         AnalyzePage('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + StringReplace(URLEncode(MovieName), '%20', '+'));
  245.       end;
  246.     end;
  247.   end
  248.   ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.5.0)');
  249. end.
  250.